//editRoleCtrl

app.controller('editRoleCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	var token = $window.localStorage.getItem("token");
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
	console.log($scope.iNowData)
	//自定义方法状态数据
	$scope.methodsStatus = [
	        {
	            "status" : "AVAILABLE",
	            "name" : "可用"
	        },
	        {
	            "status" : "FORBID",
	            "name" : "禁止"
	        },
	        {
	            "status" : "DELETE",
	            "name" : "删除"
	        }
	    ];
    angular.forEach($scope.methodsStatus,function(data,index,array){
    	if(data.status == $scope.iNowData.status){
    		$scope.itemStatus = data;
    	}
    })
    
    $scope.init = function(){
    	//初始化获取所有角色
		rest.decoJava({
	       	type: 'POST',
	        url: '/tenancy-sys/admin/role/all/list',
	        data: "",
	       	beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
	        success: function(data){
	            $scope.upRoleDatas = data.result;
	            $scope.upRoleDatas.unshift({roleName: "请选择", id: ""});
	            //遍历选中
	            var _upRoleId= "";
	            if($scope.iNowData.upRole != null){
	            	_upRoleId = $scope.iNowData.upRole.id;
	            }
	            angular.forEach($scope.upRoleDatas,function(data){
	            	if(data.id == _upRoleId){
	            		$scope.upRole = data;
	           			$scope.$apply();
	            	}
	            });
	        }
	  });
    };
    $scope.init();
    
	//保存
    $scope.preserveSubmit = function(){
    	if($scope.upRole.id == $scope.iNowData.id){
    		layer.msg("所属角色不能是自己哦",{
				offset:'t'
			});
    		return false;
    	}
    	var upId = null;
    	if($scope.upRole != null){
    		upId = $scope.upRole;
    	}
    	var formData = {
    		"id": $scope.iNowData.id,
    		"roleName": $scope.iNowData.roleName,
    		"remark": $scope.iNowData.remark,
    		"upId": $scope.upRole.id,
    		"status": $scope.itemStatus.status
    	}
    	rest.decoJava({
    		type: 'post',
            url: '/tenancy-sys/admin/role/update',  ///admin/role/save.do
            data: JSON.stringify(formData),
            contentType: "application/json; charset=utf-8",
            beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
            success: function (result) {
            	layer.closeAll('loading');
                if (result) {
                    if ("0000" == result.code) {
						layer.msg(result.message, {
				        	offset:'t',
				        	icon: 1,
							time: 0, //不自动关闭
							btn: ['确定'],
							yes: function(index){
								layer.close(index);
								$scope.closeThisDialog();
						    }
						});
                    } else {
                    	layer.msg(result.message,{
		    				offset:'t'
		    			});
                    }
                }
            }
        });
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
